const Koa = require("koa");
const app = new Koa();
const router = require("koa-router")();
const parser = require("koa-parser");
const cors = require("koa-cors"); // 允许跨域

app.use(parser());
app.use(cors());

let arr = [{
  name: '赵子琴',
  class: '高职计算机1班',
  mat:96,
  chn:93,
  eng:87
},{
  name: '钱丑棋',
  class: '高职计算机1班',
  mat:54,
  chn:62,
  eng:38
},{
  name: '孙寅书',
  class: '高职计算机1班',
  mat:93,
  chn:95,
  eng:98
},{
  name: '李卯画',
  class: '高职计算机1班',
  mat:68,
  chn:98,
  eng:76
},{
  name: '周辰笔',
  class: '高职计算机1班',
  mat:88,
  chn:75,
  eng:62
},{
  name: '吴巳墨',
  class: '高职计算机1班',
  mat:79,
  chn:89,
  eng:99
},{
  name: '郑午纸',
  class: '高职计算机1班',
  mat:98,
  chn:88,
  eng:78
},{
  name: '王未砚',
  class: '高职计算机1班',
  mat:94,
  chn:85,
  eng:76
},{
  name: '冯申梅',
  class: '高职计算机1班',
  mat:100,
  chn:94,
  eng:98
},{
  name: '陈酉兰',
  class: '高职计算机1班',
  mat:83,
  chn:84,
  eng:85
},{
  name: '褚戌竹',
  class: '高职计算机1班',
  mat:97,
  chn:97,
  eng:68
},{
  name: '卫亥菊',
  class: '高职计算机1班',
  mat:60,
  chn:77,
  eng:90
}];

router.get("/usr",async ctx=>{
    ctx.body = arr;
})
router.post("/usr",async ctx=>{
    let da = ctx.request.body.data;
    console.log(da)
    arr.push(da);
    ctx.body = true;
})
router.delete("/usr/:index",async ctx=>{
    let ind = ctx.params.index;
    arr.splice(ind,1);
    ctx.body = true;
})

router.put("/usr/:index",async ctx=>{
    let ind = ctx.params.index;
    let setUsr = ctx.request.body.usr;
    console.log(setUsr);
    arr.splice(ind,1,setUsr);
    ctx.body = true;
})
router.get("/getMod/:index",async ctx=>{
  let ind = ctx.params.index;
  let tempUsr;
  for(let i=0;i<arr.length;i++){
    if(ind == i){
      tempUsr = arr[i]
    }
  }
  ctx.body = tempUsr;
})

app.use(router.routes());
app.listen("3000",()=>{
    console.log("running 3000");
})